package com.kj.platformlogistics.common;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;


import com.kj.platformlogistics.constants.Constants;
import com.kj.platformlogistics.constants.UrlConstants;
import com.kj.platformlogistics.http.HttpRequest;
import com.kj.platformlogistics.http.HttpStringCallBack;
import com.kj.platformlogistics.main.MainActivity;
import com.kj.platformlogistics.my.model.UserInfoModel;
import com.kj.platformlogistics.util.JsonUtils;
import com.kj.platformlogistics.util.SPUtils;
import com.kj.platformlogistics.util.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MethodUtils {
    /**
     * 获取固定参数
     */
    public static Map getParams(Context context) {
        Map<String, String> map = new HashMap();
        map.put("token", getToken(context));
        return map;
    }

    /**
     * 获取token
     */
    public static String getToken(Context context) {
        String token = "";
        if (SharedPreferencesUtils.hasValue(context, Constants.ShareKey_Token)) {
            token = SharedPreferencesUtils.getSharedPreferences(context, Constants.ShareKey_Token);
        }
        return token;
    }

    /**
     * 获取登录账号
     */
    public static String getUserId(Context context) {
        String userId = "";
        if (SharedPreferencesUtils.hasValue(context, Constants.ShareKey_account)) {
            userId = SharedPreferencesUtils.getSharedPreferences(context, Constants.ShareKey_account);
        }
        return userId;
    }

    /**
     * 登录之后
     * @param context
     */
    public static void loginAfter(final Activity context, final String loginName, final String token) {
        Map<String, String> map1 = new HashMap<>();
        map1.put(Constants.ShareKey_account, loginName);
        map1.put(Constants.ShareKey_Token, token);
        SharedPreferencesUtils.saveSharedPreferences(context, map1);
        getUserInfo(context);
    }

    /**
     * 获取会员信息
     * @param context
     */
    public static void getUserInfo(final Activity context) {
        new HttpRequest(context).doPost(UrlConstants.logistics_getStationUserInfo, null,new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    int code=jsonObject.getInt("code");
                    if(code==200){
                        Object obj = JsonUtils.fromJson(result.toString(), UserInfoModel.class);
                        if (obj instanceof UserInfoModel) {
                            UserInfoModel model = (UserInfoModel) obj;
                            UserData.create(context).saveUserData(model.getResult());
                            MainActivity.actionStart(context,0);
                            SPUtils.setSharedBooleanData(context,"isLogin",true);
                        }
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) { }
        });
    }
    /**
     * 退出登录之后
     * @param context
     */
    public static void logoutAfter(final Context context) {
        SharedPreferencesUtils.delSharedPreferences(context, Constants.ShareKey_account);
        SharedPreferencesUtils.delSharedPreferences(context, Constants.ShareKey_Token);
        SharedPreferences sp = context.getSharedPreferences(UserData.USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.clear();
        ed.commit();
        SPUtils.setSharedBooleanData(context,"isLogin",false);
    }
}